<template>
	<view>
		<view class="info-box">
			<view class="info-each">
				<view class="">标题</view>
				<view class="input">
					<input type="text" v-model="info.name" placeholder="请输入物品的品牌、名称、成色等信息" />
				</view>
			</view>
			<view class="wire"></view>
			<view class="introduce-box">
				<view class="">商品描述</view>
				<view class="textarea-box">
					<textarea v-model="info.content" name="" id="" cols="30" rows="10" placeholder="请描述一下物品的品牌型号、规格、购买时间、使用情况、成色等信息"></textarea>
				</view>
			</view>
		</view>
		<view class="info-box" style="margin-top: 24rpx">
			
			<view class="wire"></view>
			<view class="info-each">
				<view class="">分类</view>
				<view class="" @click="showcate">
					<text class="tint">{{info.cate || '请选择'}}</text>
					<image src="../../static/right-img.png" mode=""></image>
				</view>
			</view>
			<view class="wire"></view>
			<view class="info-each">
				<view class="">电话</view>
				<view class="input">
					<input type="number" v-model="info.mobile" placeholder="请输入" />
				</view>
			</view>
			<view class="wire"></view>
			<view class="info-each">
				<view class="">商品价格</view>
				<view class="input">
					<input v-model="info.price" type="number" placeholder="请输入" />
					<text>元</text>
				</view>
			</view>
		</view>

		<view class="info-box" style="margin-top: 24rpx">
			<view class="info-each">
				<view class="">图片</view>
				<view class="">
					<text class="tint">请上传物品图片</text>
				</view>
			</view>
			<view class="upimg">
				<u-upload width="65" height="65" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="9">
					<image src="../../static/imgs.png" mode="widthFix" style="width: 130rpx; height: 130rpx"></image>
				</u-upload>
			</view>
		</view>
		
		<view class="info-box" style="margin-top: 24rpx">
			<view class="info-each">
				<view class="">展示视频</view>
				<view class="">
					<!-- <text class="tint">请上传展示视频</text> -->
				</view>
			</view>
			<view style="color: #999999;font-size: 22rpx;padding-top: 5rpx;">视频不要求必须上传，视频上传后，将会在平台上展示，同时半柿团队会将视频发到视频平台上帮你免费宣传</view>
			<view class="upimg">
				<u-upload
					:fileList="fileList2"
					@afterRead="afterRead"
					@delete="deletePic"
					name="2"
					multiple
					:maxCount="1"
					accept="video"
				>
				<image src="../../static/imgs.png" mode="widthFix"
					style="width: 130rpx;height: 130rpx;">
				</image>
				</u-upload>
			</view>
		</view>
		
		<view class="info-box" style="margin-top: 24rpx">
			<view class="info-each">
				<view class="">视频库视频</view>
				<view class="">
					<!-- <text class="tint">请上传视频库视频</text> -->
				</view>
			</view>
			<view style="color: #999999;font-size: 22rpx;padding-top: 5rpx;">视视频库的视频，主要用于视频平台的宣传，一个视频只能发布一次，为了更好的宣传效果，你可以将同一个信息的视频，从不同的位置拍摄多个视频上传，由半柿平台免费帮你多次宣传，快速促进您信息的成交</view>
			<view class="upimg">
				<u-upload
					:fileList="fileList3"
					@afterRead="afterRead"
					@delete="deletePic"
					name="3"
					multiple
					:maxCount="6"
					accept="video"
				>
				<image src="../../static/imgs.png" mode="widthFix"
					style="width: 130rpx;height: 130rpx;">
				</image>
				</u-upload>
			</view>
		</view>
		
		<tki-tree ref="cate" :range="ranges" confirmColor="#19B564" @confirm="confirme" />
		
		<view class="cotton"></view>
		<view class="btn-box">
			<view class="btn" @click="put">发布</view>
		</view>
		
		<TopPopup :show1="show1" :meal="meal" @padlock="padlock" @ckeck="ckeck"></TopPopup>
		
		<view class="issue" @click="toVideo">
			拍摄教程
		</view>
	</view>
</template>

<script>
	import TopPopup from "@/components/topPopup.vue"
	import tkiTree from "@/components/tki-tree/tki-tree.vue"
	import amap from '@/common/amap-wx.130.js';
export default {
	components: { TopPopup },
	data() {
		return {
			fileList1: [],
			fileList2:[],//视频 单
			fileList3:[],//视频 多
			show: false,
			ranges:[],
			info: {
				city: '',
				cate: '',
				mobile: '',
				price: '',
				content: ''
			},
			citys: '',
			city: '',
			district: '',
			lng: '',
			lat: '',
			fileList1: [],
			cate_id1: '',
			cate_id2: '',
			type: 0,
			id: '',
			config:uni.getStorageSync('config'),
			
			show1:false,
			order_id:'',
			itemid:'',
			meal: [],
			current:null,
			
			myMoney:'',
			paytype:null
		};
	},
	onLoad(op) {
		this.getcate()
		this.getcombo()
		if (op) {
			console.log(op,'???????????')
			let data = JSON.parse(op.data);
			console.log(data,'!!!!!!!!!~~~')
			this.fileList1 = data.images.map(value => {
				return {
					thumb: this.oss(value),
					url: value
				}
			})
			if(data.video){
						this.fileList2 = data.video.split(",").map(value => {
							return {
								url: this.oss(value),
								ful: value
							}
						})
					}
					if(data.videos){
						this.fileList3 = data.videos.split(",").map(value => {
							return {
								url: this.oss(value),
								ful: value
							}
						})
					}
			this.info.name = data.name
			this.info.content = data.content
			this.info.mobile = data.mobile
			this.info.price = data.price
			this.cate_id1 = data.cate_id1
			if (data.cate_id2) {
				this.cate_id2 = data.cate_id2
				this.info.cate = data.catename2 + ' ' + data.catename1
			} else {
				this.info.cate = data.catename1
			}
			this.id = data.id
			this.type = 1
		}
		this.getMoney()
		// #ifdef MP-WEIXIN
		this.paytype = 2
		// #endif
		// #ifdef APP
		this.paytype = 1
		// #endif
	},
	methods: {
		toVideo(){
			uni.navigateTo({
				url:'/pages/videoShopping/videoShopping?types=3'
			})
		},
		padlock(){
			this.show1 = false
			uni.navigateBack()
		},
		getMoney(){
			uni.$u.http.post('/getmoney ').then(res => {
				if (res.code === 1) {
					this.myMoney = res.data
				}
			}).catch((res) => {
				
			})
		},
		ckeck(e) {
			if(!e.id) return this.toast('请选择置顶套餐')
			if(this.myMoney < e.money){
				//余额不足 微信支付
				uni.$u.http.post('/meallist', {
					type:4,
					order_id: this.order_id,
					id:e.id,
					paytype:this.paytype
				}).then(res => {
					console.log(res)
					if (res.code == 1) {
						let that = this
						uni.requestPayment({
							appId: res.data.pay.appId,
							nonceStr: res.data.pay.nonceStr,
							package: res.data.pay.package,
							paySign: res.data.pay.paySign,
							signType: res.data.pay.signType,
							timeStamp: res.data.pay.timeStamp,
							success: (e) => {
								that.toast(res.msg)
								that.show1 = false
								setTimeout(() => {
									uni.navigateBack()
								},500)
							},
							fail: (e) => {
								that.$u.toast('支付失败');
							}
						})
					}
				}).catch((res) => {
						
				})
			}else {
				//余额支付
				uni.$u.http.post('/meallist', {
					type:4,
					order_id: this.order_id,
					id:e.id,
					paytype:4
				}).then(res => {
					console.log(res)
					if (res.code == 1) {
						this.toast(res.msg)
						this.show1 = false
						setTimeout(() => {
							uni.navigateBack()
						},500)
					}
				}).catch((res) => {
						
				})
			}
			
		// 	if(!this.itemid) return this.toast('请选择置顶套餐')
		// 	uni.$u.http.post('/meallist', {
		// 		type:4,
		// 		order_id: this.order_id,
		// 		id: this.itemid
		// 	}).then(res => {
		// 		console.log(res)
		// 		if (res.code == 1) {
		// 			this.toast(res.msg)
		// 			this.show1 = false
		// 			setTimeout(() => {
		// 				uni.navigateBack()
		// 			},500)
		// 		}
		// 	}).catch((res) => {
		
		// 	})
		},
		getcombo() {
			uni.$u.http.post('/meallist',{
				mealtype:4
			}).then(res => {
				if (res.code == 1) {
					this.meal = res.data
				}
			}).catch((res) => {
			})
		},
		toidle(){
			uni.navigateBack()
		},
		put() {
			console.log(this.fileList1,'....')
			if (!this.info.name) return this.toast('请输入商品标题')
			if (!this.info.content) return this.toast('请输入商品描述')
			// if (!this.city) return this.toast('请选择所在城市')
			if (!this.info.cate) return this.toast('请选择分类')
			if (!this.info.mobile) return this.toast('请输入手机号')
			if (!uni.$u.test.mobile(this.info.mobile)) return this.toast('请输入正确的手机号')
			if (!this.info.price) return this.toast('请输入商品价格')
			if (this.fileList1.length == 0) return this.toast('请上传物品图片')
		
			let imagea = [];
			this.fileList1.forEach(item => {
				console.log(item)
				imagea.push(item.url)
			})
			let video = [];
			this.fileList2.forEach(item => {
				video.push(item.url)
			})
			let videos = [];
			this.fileList3.forEach(item => {
				videos.push(item.url)
			})
		
			let datas = ''
		
			if (this.type == 0) {
				datas = {
					type: 'add',
					cate_id1: this.cate_id1,
					cate_id2: this.cate_id2,
					name: this.info.name,
					images: imagea.join(','),
					video:video.join(','),
				    videos:videos.join(','),
					price: this.info.price,
					content: this.info.content,
					// city: this.city,
					// district: this.district,
					// lng: this.lng,
					// lat: this.lat,
					id: this.id,
					mobile: this.info.mobile
				}
			} else {
				datas = {
					type: 'edit',
					id: this.id,
					cate_id1: this.cate_id1,
					cate_id2: this.cate_id2,
					name: this.info.name,
					images: imagea.join(','),
					price: this.info.price,
					content: this.info.content,
					// city: this.city,
					// district: this.district,
					// lng: this.lng,
					// lat: this.lat,
					mobile: this.info.mobile
				}
			}
		
		console.log(datas,'!!!!!!!!!!!!!!!')
			uni.$u.http.post('/postfree ', datas).then(res => {
				if (res.code == 1) {
					// this.toast(res.msg)
					this.order_id = res.data
					this.show1 = true
				}
			}).catch((res) => {
		
			})
		},
		showcate() {
			this.$refs.cate._show()
		},
		confirme(e) {
			// console.log(e[0].parents[0])
		
			if (e[0].parents[0]) {
				this.cate_id1 = e[0].parents[0].id 
				this.cate_id2 = e[0].id
				this.info.cate = e[0].parents[0].label + ' ' + e[0].label
			} else {
				this.cate_id1 = e[0].id
				this.info.cate = e[0].label
			}
		},
		getcate() {
			uni.$u.http.post('/freecate').then(res => {
				if (res.code == 1) {
					let arr = []
					for (var i = 0; i < res.data.length; i++) {
						arr.push({
							id: res.data[i].id,
							label: res.data[i].name,
							children: []
						})
						for (var a = 0; a < res.data[i].sunList.length; a++) {
							arr[i].children.push({
								id: res.data[i].sunList[a].id,
								label: res.data[i].sunList[a].name
							})
						}
		
						this.ranges = arr
						// console.log(arr)
					}
				}
			}).catch((res) => {
		
			})
		},
		open() {
			// console.log('open');
		},
		close() {
			this.show = false;
			// console.log('close');
		},
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1);
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file);

			let fileListLen = this[`fileList${event.name}`].length;
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				});
			})


			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url);

				console.log(result)


				if (result) {
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: 'success',
							message: '',
							url: result.url
						})
					)


					fileListLen++;
				} else {
					this[`fileList${event.name}`].splice(fileListLen, 1);
				}
			}
			console.log(this[`fileList${event.name}`]);
		},
		uploadFilePromise(url) {
			console.log(url);

			let that = this;
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: that.Upload.uploadurl, // 仅为示例，非真实的接口地址
					filePath: url,
					name: 'file',
					formData: {
						type: 'image',
						token: uni.getStorageSync('token')
					},
					success: (res) => {
						let data = JSON.parse(res.data)
						if (res.statusCode === 200 && data.code === 1 && data.data) {
							resolve(data.data)
						} else {
							resolve()
						}
					}
				});
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #f6f6f6;
}
.info-box {
	width: 622rpx;
	padding: 29rpx 32rpx;
	background: #ffffff;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	opacity: 1;
	margin: auto;

	.info-each {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;

		.input {
			display: flex;
			align-items: center;
			text-align: right;
			font-size: 30rpx;
			font-weight: 500;
			color: #333333;

			text {
				display: inline-block;
				padding-left: 30rpx;
			}
		}

		image {
			width: 32rpx;
			height: 32rpx;
			position: relative;
			top: 7rpx;
			left: 20rpx;
		}
		.tint {
			font-size: 30rpx;
			font-weight: 500;
			color: #999999;
		}
	}
	.upimg {
		margin-top: 30rpx;
	}

	.wire {
		width: 622rpx;
		height: 1rpx;
		background-color: #f0f0f0;
		margin: auto;
		margin: 30rpx 0;
	}

	.tags-box {
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;
		.tags {
			display: flex;
			margin-top: 24rpx;
			.tag {
				background-color: #f6f6f6;
				font-size: 30rpx;
				padding: 19rpx 48rpx 19rpx 24rpx;
				font-weight: 500;
				color: #333333;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-right: 24rpx;
				image {
					width: 30rpx;
					height: 30rpx;
					position: relative;
					top: 7rpx;
					left: 20rpx;
				}
			}
		}
	}

	.introduce-box {
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;
		.textarea-box {
			padding: 24rpx 0;
			height: 180rpx;
			font-size: 30rpx;
			font-weight: 500;
			color: #333333;
			textarea {
				width: 100%;
				height: 180rpx;
			}
		}
	}
}

.cotton {
	height: 160rpx;
}
.btn-box {
	width: 100%;
	position: fixed;
	bottom: 20rpx;

	.btn {
		width: 686rpx;
		height: 98rpx;
		background: #febf4e;
		border-radius: 49rpx 49rpx 49rpx 49rpx;
		opacity: 1;
		margin: auto;
		text-align: center;
		line-height: 98rpx;
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
	}
}

.popup-box {
	width: 560rpx;
	// height: 914rpx;
	padding-bottom: 32rpx;
	background: #FFFFFF;
	border-radius: 24rpx 24rpx 24rpx 24rpx;
	opacity: 1;
	.pop-title {
		font-size: 32rpx;
		text-align: center;
		padding-top: 60rpx;
		font-weight: bold;
		color: #333333;
	}
	.pop-hint {
		font-size: 24rpx;
		text-align: center;
		padding-top: 16rpx;
		font-weight: 500;
		color: #999999;
	}
	.content {
		width: 432rpx;
		padding: 32rpx;
		// height: 264rpx;
		background: #F6F6F6;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		opacity: 1;
		margin: auto;
		margin-top: 40rpx;
		margin-bottom: 24rpx;
		text {
			font-size: 28rpx;
			font-weight: 500;
			color: #666666;
			line-height: 50rpx;
		}
	}
	.mobile {
		font-size: 48rpx;
		font-weight: bold;
		color: #333333;
		text-align: center;
		padding-top: 16rpx;
	}
	.btn-1 {
		width: 496rpx;
		height: 80rpx;
		background: #FEBF4E;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		opacity: 1;
		text-align: center;
		line-height: 80rpx;
		font-size: 26rpx;
		font-weight: 500;
		color: #333333;
		margin: auto;
		margin-top: 48rpx;
	}
	.btn-2 {
		width: 496rpx;
		height: 80rpx;
		background: #FFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		opacity: 1;
		text-align: center;
		line-height: 80rpx;
		font-size: 26rpx;
		font-weight: 500;
		color: #333333;
		margin: auto;
		margin-top: 24rpx;
		border: 1rpx solid #F0F0F0;
	}
}


.serveBox {
				width: 690rpx;
				border-radius: 20rpx;

				.serve_tit {
					// margin-top: 30rpx;
					text-align: center;
					font-size: 36rpx;
					color: #333333;
					font-weight: bold;
					line-height: 40rpx;
				}
				
				.tc-img {
					width: 600rpx;
					margin: auto;
					margin-top:24rpx;
					image {
						width: 600rpx;
						height: 500rpx;
					}
				}
				
				.serve_cont {
					// margin: 0 30rpx;
					display: flex;
					// flex-wrap: wrap;
					// flex-direction: row;
					margin-top: 24rpx;
					overflow: hidden;
					overflow-x: auto;
					white-space: nowrap;

					.serve {
						margin: 30rpx 0;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						margin-right: 20rpx;
						padding: 16rpx 32rpx;
						// width: 190rpx;
						// height: 200rpx;
						box-sizing: border-box;
						border-radius: 20rpx;
						border: 1rpx solid #989898;


						.serve_num {
							margin-bottom: 20rpx;
							font-size: 32rpx;
							font-weight: bold;
							color: #333;
							line-height: 45rpx;
						}

						.serve_hour {
							font-size: 28rpx;
							font-weight: 400;
							color: #323232;
							line-height: 40rpx;
						}
					}

					.active {
						// border: 1rpx solid #FFDF41;
						border: 2px solid #FF7A7A !important;
					}

				}
				
				.tc-btn-box {
					width: 560rpx;
					margin: auto;
					margin-top: 20rpx;
					display: flex;
					justify-content: space-between;
					.tc-btn1 {
						width: 220rpx;
						height: 80rpx;
						background-color: #FBBA45;
						border-radius: 10rpx;
						text-align: center;
						line-height: 80rpx;
					}
					.tc-btn2 {
						width: 220rpx;
						height: 80rpx;
						border-radius: 10rpx;
						text-align: center;
						line-height: 80rpx;
						background-color: #9A9A9A;
						color: #FFF;
					}
				}
			}
			
			
			.issue {
					width: 60rpx;
					height: 60rpx;
					background: linear-gradient(180deg, #FECB6F 0%, #FFAE1C 100%);
					border-radius: 0rpx 0rpx 0rpx 0rpx;
					opacity: 1;
					border-radius: 50%;
					font-size: 24rpx;
					font-weight: bold;
					color: #333333;
					padding: 20rpx;
					text-align: center;
					position: fixed;
					right: 32rpx;
					// bottom: 200rpx;
					bottom: 48%;
					z-index: 9;
				}
</style>
